package com.bite.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@SpringBootApplication
public class CaptchaDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(CaptchaDemoApplication.class, args);
/*		HashMap<String,Integer> map = new HashMap<>();
		String str ="abcd";
		String str1 = str.substring(0,3);
		System.out.println(str1);
		HashMap<String,Integer> map2 = new HashMap<>();
		map2.put("bar",1);
		map2.put("foo",1);
		String s = "barfoofoobarthefoo";
		for (int i = 0,j=0; i < s.length(); i+=3) {
			String ss = s.substring(i,i+3);
			map.put(ss,map.getOrDefault(ss,0)+1);
			if(i-j>map2.size()*3) {
				String ssj = s.substring(j,j+3);
				map.put(ssj,map.getOrDefault(ssj,0)-1);
				if(map.get(ssj)==0) {
					map.remove(ssj);
				}
				j+=3;
			}
			if(map.equals(map2)) {
				System.out.println("==");
			}

		}
		System.out.println(map);*/
		/*String s = "wordgoodgoodgoodbestword";
		String[] words ={"word","good","best","good"};
		System.out.println(findSubstring(s, words));*/
	}
	public static List<Integer> findSubstring(String s, String[] words) {
		List<Integer> list = new ArrayList<>();
		HashMap<String,Integer> hash1 = new HashMap<>();
		int length = 0;;
		for(int i = 0;i < words.length;i++) {
			length = words[i].length();
			hash1.put(words[i],hash1.getOrDefault(words[i],0)+1);
		}
		HashMap<String,Integer> hash2 = new HashMap<>();
		for(int right = 0,left = 0;right<s.length();right+=length) {
			String ss = s.substring(right,right+length);
			hash2.put(ss,hash2.getOrDefault(ss,0)+1);
			if(right-left+1>hash1.size()*length) {
				String ssl = s.substring(left,left+length);
				hash2.put(ssl,hash2.getOrDefault(ssl,0)-1);
				if(hash2.get(ssl)==0) {
					hash2.remove(ssl);
				}
				left+=length;
			}
			if(hash1.equals(hash2)) {
				list.add(left);
			}

		}
		return list;

	}

}
